{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit Radio;

interface

uses AVRenderer, Visualisation;

type
  TAHMRadioRenderer = class(TAHMVisualisationRenderer)
  private
  protected
    function GetDuration: LongInt; override;
    function GetPosition: LongInt; override;
    procedure SetPosition(Position: LongInt); override;
  public
    function Play: Boolean; override;
    function FastForward: Boolean; override;
    function Rewind: Boolean; override;
  end;

implementation

uses ApplicationManager, DirectShowManager;

function TAHMRadioRenderer.GetDuration: LongInt;
begin
  // Not supported for radio
  Result := 0;
end;

function TAHMRadioRenderer.GetPosition: LongInt;
begin
  // Not supported for radio
  Result := 0;
end;

procedure TAHMRadioRenderer.SetPosition(Position: LongInt);
begin
  // Not supported for radio
end;

function TAHMRadioRenderer.Play: Boolean;
begin
  // Set default result - couldn't play
  Result := False;

  if Assigned(Playlist.CurrentItem) then
  begin
    with DirectShowInstance do
    begin
      // Check if audio renderer is currently in use
      if Assigned(DirectShowAudioRenderer) then
      begin
        // In use - if we own it then just resume playback
        if DirectShowAudioRenderer.Owner = Self then
        begin
          DirectShowAudioRenderer.Play;
          if not Muted then DirectShowAudioRenderer.InternalUnMute;
        end
        else
        begin
          // Owned by other stream, so nick it, but inform other stream first
          TAHMAVRenderer(DirectShowAudioRenderer.Owner).SignalMediaEvent(msigAbort);
          StopAudio;
        end;
      end;

      // Check if radio renderer needs initialising
      if not Assigned(DirectShowAudioRenderer) then
      begin
        // Tune radio to appropriate station
        with ApplicationManagerInstance.MediaManager.Radio do
          if not TuneRadio(Playlist.CurrentItem.URL) then
        begin
          LastError := meInstallationBroken;
          Exit;
        end;

        if not PlayRadio(Playlist.CurrentItem.URL) then
        begin
          // Map DirectX audio error into one of our generic error types
          case DirectShowInstance.LastAudioError of
            daOkay:       LastError := meNone;
            daMissingWMP: LastError := meInstallationBroken;
            daUnknown:    LastError := meUnsupportedMedia;
          end;
          Exit;
        end;

        DirectShowAudioRenderer.InternalVolume := ConvertVolume(Volume);
        if Muted then DirectShowAudioRenderer.InternalFastMute;
        DirectShowAudioRenderer.Owner := Self;
      end;
      Result := True;
    end;
  end;
end;

function TAHMRadioRenderer.FastForward: Boolean;
begin
  // Radio doesn't support fast forward, so we seek next station instead
  with ApplicationManagerInstance.MediaManager.Radio do
    Result := SeekUp;
end;

function TAHMRadioRenderer.Rewind: Boolean;
begin
  // Radio doesn't support rewind, so we seek previous station instead
  with ApplicationManagerInstance.MediaManager.Radio do
    Result := SeekDown;
end;

end.
